package com.wj.algorithm.chainedlist.oneway;

import com.wj.algorithm.chainedlist.ChainNode;

public class OneWayChainedList {

    private ChainNode head;
    
    private ChainNode tail;
    
    public OneWayChainedList() {
        
    }

    public ChainNode getHead() {
        return head;
    }

    public void setHead(ChainNode head) {
        this.head = head;
    }

    public ChainNode getTail() {
        return tail;
    }

    public void setTail(ChainNode tail) {
        this.tail = tail;
    }
    
    public void addToTail(ChainNode node) {
        if(tail == null) {
            head = node;
            tail = node;
            head.setNext(tail);
            tail.setNext(null);
            return;
        }
        tail.setNext(node);
    }
    public void addToHead(ChainNode node) {
        if(head == null) {
            head = node;
            tail = node;
            head.setNext(tail);
            tail.setNext(null);
            return;
        }
        node.setNext(head);
        head = node;
    }
    public void deleteNode(ChainNode node) {
        if(head == null) {
            return;
        }
        ChainNode prev = head;
        ChainNode current = prev.getNext();
        if(prev == node) {
            head = current;
            return;
        }
        while(current != null) {
            if(current == node) {
                prev.setNext(current.getNext());
                return;
            }
            prev = current.getNext();
            current = prev.getNext();
        }
    }
    
    public static void main(String[] args) {
        OneWayChainedList chainedList = new OneWayChainedList();
        
        chainedList.addToHead(new ChainNode(3));
    }
}
